package com.javaweb.dao.impl;

import com.javaweb.common.StringUtils;
import com.javaweb.dao.PermDao;
import com.javaweb.model.Category;
import com.javaweb.model.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/24 22:43
 */

/**
 * todo: 实现全部如下方法
 */
@Repository
@Slf4j
public class PermDaoImpl implements PermDao {
    @Autowired
    private Connection conn ;
    @Autowired
    private JdbcTemplate jdbcTemplate ;

    private RowMapper<Permission> categoryRowMapper = new RowMapper<Permission>() {
        @Override
        public Permission mapRow(ResultSet rs, int rowNum) throws SQLException {
            Permission permission = new Permission()
                    .setName(rs.getString("name"))
                    .setUrl(rs.getString("url"))
                    .setPerId(rs.getInt("per_id"));
            return permission;
        }
    };
    @Override
    public Integer insert(Permission perm) throws SQLException {
        String sql="insert into tb_permission(per_id,name,url) values(?,?,?)";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setInt(1,perm.getPerId());
            ps.setString(2,perm.getName());
            ps.setString(3,perm.getUrl());
            return ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error("execute insert perm error: " + e.getMessage()) ;
        }
        return 0 ;
    }

    @Override
    public Integer insertRolePerm(int roleId, int permId) throws SQLException {
        String sql="insert into tb_role_per(id,role_id,perm_id) values(?,?,?)";
        try(PreparedStatement ps = this.conn.prepareStatement(sql)){
            ps.setInt(1,roleId);
            ps.setInt(2,permId);
            ps.setInt(3,permId);
            return ps.executeUpdate() ;
        }catch (Exception e) {
            log.error("execute insert role_perm error: " + e.getMessage()) ;
        }
        return 0;
    }

    @Override
    public Permission selectOne(String name, String url) throws SQLException {
        String sql="select * from tb_permission where name=? and url=?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;) {
            ps.setString(1,name);
            ps.setString(2,url);
            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Permission perm = new Permission()
                        .setName(name)
                        .setUrl(url);
                return perm;
            }
        } catch (SQLException e) {
            log.error("selectOne error: " + e.getMessage()) ;
        }
        return null;
    }

    @Override
    public Permission selectOneById(int permId) throws SQLException {
        String sql="select * from tb_permission where per_id=?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)){
            ps.setInt(1,permId);
            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Permission perm = new Permission()
                        .setPerId(permId);
                return perm;
            }
        }catch (SQLException e){
            log.error("selectOneById error: " + e.getMessage()) ;
        }
        return null;
    }
//   多表查询
    @Override
    public List<Permission> selectPermsByRoleName(String roleName) throws SQLException {
        String sql="select p.* from tb_permission p join tb_role_per tp on p.per_id=tp.per_id join tb_role r on r.role_id=tp.role_id " +
                "where r.name=?";
        List<Permission> permlist=new ArrayList<Permission>();
        try(PreparedStatement ps =this.conn.prepareStatement(sql);){
            ps.setString(1,roleName);
            ResultSet rs = ps.executeQuery() ;
            while (rs.next()) {
                Permission perm = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setUrl(rs.getString(3));
                permlist.add(perm);
            }
            return permlist;
        }catch (SQLException e){
            log.error("execute selectPermsByRoleName:"+e.getMessage());
        }
        return null;
    }
//  多表查询
    @Override
    public List<Permission> selectPermsByRoleId(int roleId) throws SQLException {
        String sql="select p.* from tb_permission p join tb_role_per tp on p.per_id=tp.per_id where role_id=?";
        List<Permission> permlist=new ArrayList<Permission>();
        try(PreparedStatement ps =this.conn.prepareStatement(sql);){
            ps.setInt(1,roleId);
            ResultSet rs = ps.executeQuery() ;
            while (rs.next()) {
                Permission perm = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setUrl(rs.getString(3));
                permlist.add(perm);
            }
        }catch (SQLException e){
            log.error("execute selectPermsByRoleId:"+e.getMessage());
        }
        return null;
    }

    @Override
    public Integer deleteById(int permId) throws SQLException {
        String sql="delete from tb_permission where per_id=?";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)){
            ps.setInt(1,permId);
            return ps.executeUpdate() ;
        }catch (Exception e) {
            log.error("deleteById error: " + e.getMessage()) ;
        }
        return null;
    }
//void返回值
    @Override
    public void updateByPerm(Permission perm) throws SQLException {
        String sql = "UPDATE tb_permission SET name = ?, url = ? WHERE per_id = ?";
        jdbcTemplate.update(sql, new Object[]{perm.getName(), perm.getUrl(), perm.getPerId()});
    }
}
